

export default class TextScroll extends Laya.Script {
    private text: Laya.Text;

    private mouseX: number = 0;
    private mouseY: number = 0;
    private isMove: boolean = false;

    onAwake(): void {
        this.text = this.owner as Laya.Text;
    }

    onMouseDown(e: Laya.Event): void {
        this.mouseX = this.text.mouseX;
        this.mouseY = this.text.mouseY;
        this.isMove = true;
    }

    onMouseMove(e: Laya.Event): void {
        if (!this.isMove) return;
        let deltaX = this.mouseX - this.text.mouseX;
        let deltaY = this.mouseY - this.text.mouseY;
        this.text.scrollX = Math.max(0, Math.min(this.text.maxScrollX, this.text.scrollX + deltaX));
        this.text.scrollY = Math.max(0, Math.min(this.text.maxScrollY, this.text.scrollY + deltaY));
        this.mouseX = this.text.mouseX;
        this.mouseY = this.text.mouseY;
    }

    onMouseUp(e: Laya.Event): void {
        this.isMove = false;
    }
}